package com.example.demo.aop;


import com.example.demo.pojo.Learn;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Created by dingkl on 2017/7/14.
 */
@Aspect
@Order(value = 1)
@Component
public class TestAop {

    private Logger logger = LoggerFactory.getLogger(TestAop.class.getName());


    //@Pointcut(value = "execution(* com.example.demo.service.LearnService.add(..))&& args(learn)",argNames = "learn")
    //public void beforePointcut(Learn learn){
    @Pointcut(value = "execution(* com.example.demo.service.LearnService.add(..))")
    public void beforePointcut(){
    }

    //@Before(value = "beforePointcut(learn)",argNames = "learn")
    //public void before(Learn learn){
    @Before(value = "beforePointcut()")
    public void before(){
        logger.info("before");
    }

    //@AfterReturning(value="beforePointcut(learn)",argNames="learn", returning="learn")
    //public void after_running(Learn learn){
    @AfterReturning(value="beforePointcut()")
    public void after_running(){
        logger.info("after_running");
    }

    @AfterThrowing(value = "beforePointcut()")
    public void after_throwing(){
        logger.info("after_throwing");
    }

    //@AfterReturning(value="beforePointcut(learn)", argNames="learn")
    //public void after(Learn learn){
    @After(value="beforePointcut()")
    public void after(){
        logger.info("after");
    }

    //注意返回值
    @Around(value = "beforePointcut()")
    public int round(ProceedingJoinPoint point){
        int  i = 0;
        try {
            logger.info("round start");
            i =(Integer)point.proceed();
            logger.info("round end");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return i;
    }
}
